<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="firstName">
        <el-input
          v-model="queryParams.firstName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobilePhone">
        <el-input
          v-model="queryParams.mobilePhone"
          placeholder="请输入手机"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工号" prop="employeeId">
        <el-input
          v-model="queryParams.employeeId"
          placeholder="请输入工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ehr:employee:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ehr:employee:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ehr:employee:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ehr:employee:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="employeeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="员工ID" align="center" prop="partyId" /> -->
      <el-table-column label="姓名" align="center" prop="firstName" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="部门" align="center" prop="deptId" />
      <el-table-column label="职位" align="center" prop="positionId" />
      <el-table-column label="手机" align="center" prop="mobilePhone" />
      <el-table-column label="身份证号" align="center" prop="cardId" />
      <el-table-column label="工号" align="center" prop="employeeId" />
  <el-table-column label="入职日期" align="center" prop="entryDate" width="180">
    <template slot-scope="scope">
      <span>{{ parseTime(scope.row.entryDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
    </template>
  </el-table-column>
      <el-table-column label="邮箱" align="center" prop="mail" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ehr:employee:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ehr:employee:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="850px" append-to-body>
      <el-form ref="form" :inline="true" :label-position="labelPosition" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="姓名" prop="firstName">
          <el-input v-model="form.firstName" placeholder="请输入姓名" :style="{ width: '250px' }"/>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别" :style="{ width: '250px' }" />
        </el-form-item>
        <el-form-item label="职位" prop="positionId">
          <el-input v-model="form.positionId" placeholder="请输入职位" :style="{ width: '250px' }"  />
        </el-form-item>
        <el-form-item label="手机" prop="mobilePhone">
          <el-input v-model="form.mobilePhone" placeholder="请输入手机" :style="{ width: '250px' }"  />
        </el-form-item>
        <el-form-item label="身份证号" prop="cardId">
          <el-input v-model="form.cardId" placeholder="请输入身份证号" :style="{ width: '250px' }"  />
        </el-form-item>
        <el-form-item label="工号" prop="employeeId">
          <el-input v-model="form.employeeId" placeholder="请输入工号" :style="{ width: '250px' }"  />
        </el-form-item>
  <el-form-item label="入职日期" prop="entryDate">
    <el-date-picker clearable
                    v-model="form.entryDate"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    default-time = "00:00:00"
                    placeholder="请选择入职日期" :style="{ width: '250px' }" >
    </el-date-picker>
  </el-form-item>
        <el-form-item label="邮箱" prop="mail">
          <el-input v-model="form.mail" placeholder="请输入邮箱" :style="{ width: '250px' }"  />
        </el-form-item>
        <el-form-item label="电话" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入电话" :style="{ width: '250px' }"  />
        </el-form-item>
        <el-form-item label="头像" prop="imageData">
          <el-input v-model="form.imageData" type="textarea" placeholder="请输入内容" :style="{ width: '250px' }"  />
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入民族" :style="{ width: '250px' }"  />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker clearable
            v-model="form.birthDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出生日期"  :style="{ width: '250px' }" >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="证件有效期" prop="cardDueDate">
          <el-input v-model="form.cardDueDate" placeholder="请输入证件有效期"  :style="{ width: '250px' }" />
        </el-form-item>
        <el-form-item label="籍贯" prop="nativePlace">
          <el-input v-model="form.nativePlace" placeholder="请输入籍贯"  :style="{ width: '250px' }" />
        </el-form-item>
        <el-form-item label="现居住地址" prop="residentialAddress">
          <el-input v-model="form.residentialAddress" placeholder="请输入现居住地址" :style="{ width: '250px' }"  />
        </el-form-item>
        <el-form-item label="家庭住址" prop="homeAddress">
          <el-input v-model="form.homeAddress" placeholder="请输入家庭住址" :style="{ width: '250px' }"  />
        </el-form-item>
        <el-form-item label="最高学历" prop="education">
          <el-input v-model="form.education" placeholder="请输入最高学历"  :style="{ width: '250px' }" />
        </el-form-item>
        <el-form-item label="健康状况" prop="health">
          <el-input v-model="form.health" placeholder="请输入健康状况"  :style="{ width: '250px' }" />
        </el-form-item>
        <el-form-item label="紧急联系人姓名" prop="emergencyPerson">
          <el-input v-model="form.emergencyPerson" placeholder="请输入紧急联系人姓名" :style="{ width: '250px' }"  />
        </el-form-item>
        <el-form-item label="紧急联系人电话" prop="emergencyPhone">
          <el-input v-model="form.emergencyPhone" placeholder="请输入紧急联系人电话" :style="{ width: '250px' }"  />
        </el-form-item>
        <el-form-item label="个人邮箱" prop="personalEmail">
          <el-input v-model="form.personalEmail" placeholder="请输入个人邮箱" :style="{ width: '250px' }"  />
        </el-form-item>
        <el-form-item label="员工状态" prop="emplStatusId">
          <el-input v-model="form.emplStatusId" placeholder="请输入员工状态"  :style="{ width: '250px' }" />
        </el-form-item>
        <el-form-item label="试用期限" prop="probationPeriod">
          <el-input v-model="form.probationPeriod" placeholder="请输入试用期限" :style="{ width: '250px' }"  />
        </el-form-item>
  <el-form-item label="转正日期" prop="regularDate">
    <el-date-picker clearable
                    v-model="form.regularDate"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    default-time = "00:00:00"
                    placeholder="请选择转正日期" :style="{ width: '250px' }" >
    </el-date-picker>
  </el-form-item>
        <el-form-item label="入职公司" prop="orgId">
          <el-input v-model="form.orgId" placeholder="请输入入职公司" :style="{ width: '250px' }"  />
        </el-form-item>
        <el-form-item label="职级" prop="bandLevel">
          <el-input v-model="form.bandLevel" placeholder="请输入职级" :style="{ width: '250px' }"  />
        </el-form-item>
        <el-form-item label="参加工作时间" prop="joinWorkTime">
          <el-date-picker clearable
            v-model="form.joinWorkTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择参加工作时间" :style="{ width: '250px' }" >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="comments">
          <el-input v-model="form.comments" placeholder="请输入备注"  :style="{ width: '250px' }" />
        </el-form-item>
  <el-form-item label="最后更新时间" prop="lastUpdatedStamp">
    <el-date-picker clearable
                    v-model="form.lastUpdatedStamp"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    default-time = "00:00:00"
                    placeholder="请选择最后更新时间" :style="{ width: '250px' }" >
    </el-date-picker>
  </el-form-item>
  <el-form-item label="续签时间" prop="renewContractDate">
    <el-date-picker clearable
                    v-model="form.renewContractDate"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    default-time = "00:00:00"
                    placeholder="请选择续签时间" :style="{ width: '250px' }" >
    </el-date-picker>
  </el-form-item>
  <el-form-item label="合同签订日期" prop="signContractDate">
    <el-date-picker clearable
                    v-model="form.signContractDate"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    default-time = "00:00:00"
                    placeholder="请选择合同签订日期" :style="{ width: '250px' }" >
    </el-date-picker>
  </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmployee, getEmployee, delEmployee, addEmployee, updateEmployee } from "@/api/ehr/employee";

export default {
  name: "Employee",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工信息表格数据
      employeeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        firstName: null,
        deptId: null,
        mobilePhone: null,
        employeeId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询员工信息列表 */
    getList() {
      this.loading = true;
      listEmployee(this.queryParams).then(response => {
        this.employeeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        partyId: null,
        firstName: null,
        gender: null,
        deptId: null,
        positionId: null,
        mobilePhone: null,
        cardId: null,
        employeeId: null,
        entryDate: null,
        mail: null,
        telephone: null,
        imageData: null,
        nation: null,
        birthDate: null,
        cardType: null,
        cardDueDate: null,
        nativePlace: null,
        residentialAddress: null,
        homeAddress: null,
        education: null,
        politicStatus: "0",
        maritalStatus: "0",
        health: null,
        emergencyPerson: null,
        emergencyPhone: null,
        personalEmail: null,
        emplStatusId: null,
        probationPeriod: null,
        regularDate: null,
        orgId: null,
        bandLevel: null,
        joinWorkTime: null,
        comments: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null,
        renewContractDate: null,
        contractType: null,
        signContractDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.partyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const partyId = row.partyId || this.ids
      getEmployee(partyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.partyId != null) {
            updateEmployee(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEmployee(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const partyIds = row.partyId || this.ids;
      this.$modal.confirm('是否确认删除员工信息编号为"' + partyIds + '"的数据项？').then(function() {
        return delEmployee(partyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehr/employee/export', {
        ...this.queryParams
      }, `employee_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
